<?php
/**
 * udalost_sabl Model for Ohlasky Component
 *
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * udalost_sabl  Model
 */
class OhlaskysModelUdalost_sabl extends JModel {
/**
 * Constructor that retrieves the ID from the request
 *
 * @access	public
 * @return	void
 */
    function __construct() {
        parent::__construct();

        $array = JRequest::getVar('cid',  0, '', 'array');
        $this->setId((int)$array[0]);
    }

    /**
     * Method to set the udalost_sabl identifier
     *
     * @access	public
     * @param	int udalost_sabl identifier
     * @return	void
     */
    function setId($id) {
    // Set id and wipe data
        $this->_id		= $id;
        $this->_data	= null;
    }

    /**
     * Method to get a udalost_sabl
     * @return object with data
     */
    function &getData() {
    // Load the data
        if (empty( $this->_data )) {
            $query = ' SELECT * FROM #__udalost_sabl '.
                '  WHERE id = '.$this->_id;
            $this->_db->setQuery( $query );
            $this->_data = $this->_db->loadObject();
            if (is_object($this->_data)) {
                $this->_data->misto_nazev = $this->getNazevMista($this->_data->misto_id);
                $this->_data->sablona_nazev = $this->getNazevSablony($this->_data->sablona_id);
            }
        }
        if (!$this->_data) {
            $this->_data = new stdClass();
            $this->_data->id = 0;
            $this->_data->sablona_id = $this->getSablona_id_form();
            $this->_data->misto_id = $this->getMisto_id_form();
            $this->_data->misto_nazev = $this->getNazevMista($this->_data->misto_id);
            $this->_data->sablona_nazev = $this->getNazevSablony($this->_data->sablona_id);
            $this->_data->den_tydne = $this->getDen_tydne_form();
            $this->_data->cas = null;
            $this->_data->cas_konec = null;
            $this->_data->hruby_popis = null;
            $this->_data->zvyrazneni = 0;
        }
        return $this->_data;
    }

    /**
     * Method to store a record
     *
     * @access	public
     * @return	boolean	True on success
     */
    function store() {
        $row =& $this->getTable();

        $data = JRequest::get( 'post' );

        // Bind the form fields to the udalost_sabl table
        if (!$row->bind($data)) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Make sure the udalost_sabl record is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Store the web link table to the database
        if (!$row->store()) {
            $this->setError( $row->getError() );
            return false;
        }

        return true;
    }

    /**
     * Method to delete record(s)
     *
     * @access	public
     * @return	boolean	True on success
     */
    //misto nelze vymazat, metoda nebude pouzivana
    function delete() {
        $uspech_bool = false;
        $del_udalost_sabl_id = JRequest::getVar( 'del_udalost_sabl_id', 0 );
        if (!empty($del_udalost_sabl_id)) {
            $row =& $this->getTable();
            $del_udalost_sabl_id = intval($del_udalost_sabl_id);
            if (!$row->delete( $del_udalost_sabl_id )) {
                $this->setError( $row->getErrorMsg() );
            } else {
                $uspech_bool = true;
            }
        }
        return $uspech_bool;
    }

    function getSablona_id_form() {
        $sablona_id = JRequest::getVar('sablona_id',  0, '', 'integer');
        return $sablona_id;
    }

    function getMisto_id_form() {
        $misto_id = JRequest::getVar('misto_id',  0, '', 'integer');
        return $misto_id;
    }

    function getDen_tydne_form() {
        $den_tydne = JRequest::getVar('den_tydne',  '');
        return $den_tydne;
    }

    function getNazevMista(&$misto_id) {
        $nazev_mista = '';
        if (!empty($misto_id)) {
            $query = ' SELECT * FROM #__mista '.
                '  WHERE id = '.$misto_id;
            $this->_db->setQuery( $query );
            $misto_obj = $this->_db->loadObject();
            if (is_object($misto_obj)) {
                $nazev_mista = $misto_obj->misto;
            }
        }
        return $nazev_mista;
    }

    function getNazevSablony(&$sablona_id) {
        $nazev_sablony = '';
        if (!empty($sablona_id)) {
            $query = ' SELECT * FROM #__sablony '.
                '  WHERE id = '.$sablona_id;
            $this->_db->setQuery( $query );
            $sablona_obj = $this->_db->loadObject();
            if (is_object($sablona_obj)) {
                $nazev_sablony = $sablona_obj->nazev;
            }
        }
        return $nazev_sablony;
    }

}
